<template>
  <view class="user-container">
    <!-- 用户信息头部 -->
    <view class="user-header">
      <view class="header-bg"></view>
      <view class="user-info">
        <view class="avatar-container">
          <image 
            class="avatar" 
            :src="userInfo.avatar || 'http://gips0.baidu.com/it/u=1690853528,2506870245&fm=3028&app=3028&f=JPEG&fmt=auto?w=1024&h=1024'" 
            mode="aspectFill"
          ></image>
          <view class="avatar-ring"></view>
        </view>
        <view class="info">
          <view class="nickname">{{ userInfo.nickname || userInfo.username || '用户' }}</view>
          <view class="phone">{{ userInfo.phone || '未绑定手机号' }}</view>
          <view class="level-badge">
            <uni-icons type="vip-filled" size="14" color="#FFD700"></uni-icons>
            <text>VIP会员</text>
          </view>
        </view>
        <view class="settings-btn" @tap="gotoSettings">
          <uni-icons type="gear" size="20" color="rgba(255,255,255,0.8)"></uni-icons>
        </view>
      </view>
      
      <view class="user-stats">
        <view class="stat-item" @tap="gotoPoints">
          <text class="number">0</text>
          <text class="label">积分</text>
        </view>
        <view class="stat-divider"></view>
        <view class="stat-item" @tap="gotoCoupons">
          <text class="number">0</text>
          <text class="label">优惠券</text>
        </view>
        <view class="stat-divider"></view>
        <view class="stat-item" @tap="gotoFavorites">
          <text class="number">0</text>
          <text class="label">收藏</text>
        </view>
      </view>
    </view>

    <!-- 订单管理 -->
    <view class="section order-section">
      <view class="section-header">
        <view class="section-title">
          <uni-icons type="list" size="18" color="#667eea"></uni-icons>
          <text>我的订单</text>
        </view>
        <view class="more-btn" @tap="gotoOrderList">
          <text>查看全部</text>
          <uni-icons type="right" size="14" color="#999"></uni-icons>
        </view>
      </view>
      
      <view class="order-tabs">
        <view class="tab-item" @tap="gotoOrderList('pending')">
          <view class="tab-icon pending">
            <uni-icons type="wallet" size="24" color="#FF9500"></uni-icons>
          </view>
          <text>待付款</text>
        </view>
        <view class="tab-item" @tap="gotoOrderList('shipped')">
          <view class="tab-icon shipped">
            <uni-icons type="paperplane" size="24" color="#007AFF"></uni-icons>
          </view>
          <text>待收货</text>
        </view>
        <view class="tab-item" @tap="gotoOrderList('completed')">
          <view class="tab-icon completed">
            <uni-icons type="checkmarkempty" size="24" color="#34C759"></uni-icons>
          </view>
          <text>已完成</text>
        </view>
        <view class="tab-item" @tap="gotoOrderList('refund')">
          <view class="tab-icon refund">
            <uni-icons type="undo" size="24" color="#FF3B30"></uni-icons>
          </view>
          <text>退换货</text>
        </view>
      </view>
    </view>

    <!-- 功能菜单 -->
    <view class="section menu-section">
      <view class="menu-grid">
        <view class="menu-item" @tap="gotoAddressList">
          <view class="menu-icon address">
            <uni-icons type="location" size="22" color="#FF6B6B"></uni-icons>
          </view>
          <view class="menu-content">
            <text class="menu-title">收货地址</text>
            <text class="menu-desc">管理收货地址</text>
          </view>
          <uni-icons type="right" size="14" color="#C7C7CC"></uni-icons>
        </view>
        
        <view class="menu-item" @tap="gotoFavorites">
          <view class="menu-icon favorite">
            <uni-icons type="heart" size="22" color="#FF69B4"></uni-icons>
          </view>
          <view class="menu-content">
            <text class="menu-title">我的收藏</text>
            <text class="menu-desc">收藏的商品</text>
          </view>
          <uni-icons type="right" size="14" color="#C7C7CC"></uni-icons>
        </view>
        
        <view class="menu-item" @tap="gotoWallet">
          <view class="menu-icon wallet">
            <uni-icons type="wallet" size="22" color="#32CD32"></uni-icons>
          </view>
          <view class="menu-content">
            <text class="menu-title">我的钱包</text>
            <text class="menu-desc">余额和账单</text>
          </view>
          <uni-icons type="right" size="14" color="#C7C7CC"></uni-icons>
        </view>
        
        <view class="menu-item" @tap="contactService">
          <view class="menu-icon service">
            <uni-icons type="chatboxes" size="22" color="#1E90FF"></uni-icons>
          </view>
          <view class="menu-content">
            <text class="menu-title">客服中心</text>
            <text class="menu-desc">在线客服支持</text>
          </view>
          <uni-icons type="right" size="14" color="#C7C7CC"></uni-icons>
        </view>
      </view>
    </view>

    <!-- 其他功能 -->
    <view class="section other-section">
      <view class="menu-grid">
        <view class="menu-item" @tap="gotoHelp">
          <view class="menu-icon help">
            <uni-icons type="help" size="22" color="#9370DB"></uni-icons>
          </view>
          <view class="menu-content">
            <text class="menu-title">帮助中心</text>
            <text class="menu-desc">常见问题</text>
          </view>
          <uni-icons type="right" size="14" color="#C7C7CC"></uni-icons>
        </view>
        
        <view class="menu-item" @tap="showAbout">
          <view class="menu-icon about">
            <uni-icons type="info" size="22" color="#FFA500"></uni-icons>
          </view>
          <view class="menu-content">
            <text class="menu-title">关于我们</text>
            <text class="menu-desc">了解我们</text>
          </view>
          <uni-icons type="right" size="14" color="#C7C7CC"></uni-icons>
        </view>
        
        <!-- 微信授权测试入口 -->
        <view class="menu-item" @tap="goToWechatAuthTest">
          <view class="menu-icon wechat">
            <uni-icons type="weixin" size="22" color="#07C160"></uni-icons>
          </view>
          <view class="menu-content">
            <text class="menu-title">微信授权测试</text>
            <text class="menu-desc">测试微信登录功能</text>
          </view>
          <uni-icons type="right" size="14" color="#C7C7CC"></uni-icons>
        </view>
      </view>
    </view>

    <!-- 退出登录 -->
    <view class="section logout-section">
      <button class="logout-btn" @tap="handleLogout">
        <uni-icons type="loop" size="18" color="#fff"></uni-icons>
        <text>退出登录</text>
      </button>
    </view>

    <!-- 底部安全区域 -->
    <view class="bottom-safe"></view>
  </view>
</template>

<script>
import { getUserInfo } from '@/utils/auth'
import { logout } from '@/utils/auth'

export default {
  data() {
    return {
      userInfo: {}
    }
  },

  onLoad() {
    this.loadUserInfo()
  },

  onShow() {
    this.loadUserInfo()
  },

  methods: {
    // 加载用户信息
    loadUserInfo() {
      this.userInfo = getUserInfo() || {}
    },

    // 跳转到订单列表
    gotoOrderList(type = 'all') {
      uni.navigateTo({
        url: `/src/pages/order/list?status=${type}`
      })
    },

    // 跳转到地址管理
    gotoAddressList() {
      uni.navigateTo({
        url: '/src/pages/address/list'
      })
    },

    // 跳转到我的收藏
    gotoFavorites() {
      uni.navigateTo({
        url: '/src/pages/favorite/index'
      })
    },

    // 跳转到钱包
    gotoWallet() {
      uni.navigateTo({
        url: '/src/pages/wallet/index'
      })
    },

    // 跳转到积分
    gotoPoints() {
      uni.navigateTo({
        url: '/src/pages/points/index'
      })
    },

    // 跳转到优惠券
    gotoCoupons() {
      uni.navigateTo({
        url: '/src/pages/coupon/list'
      })
    },

    // 跳转到设置
    gotoSettings() {
      uni.navigateTo({
        url: '/src/pages/user/setting'
      })
    },

    // 跳转到帮助中心
    gotoHelp() {
      uni.navigateTo({
        url: '/src/pages/help/center'
      })
    },

    // 联系客服
    contactService() {
      uni.showToast({
        title: '客服功能开发中',
        icon: 'none'
      })
    },

    // 关于我们
    showAbout() {
      uni.navigateTo({
        url: '/src/pages/about/index'
      })
    },

    // 跳转到微信授权测试页面
    goToWechatAuthTest() {
      uni.navigateTo({
        url: '/src/pages/wechat-test/index'
      })
    },

    // 退出登录
    handleLogout() {
      uni.showModal({
        title: '提示',
        content: '确定要退出登录吗？',
        success: (res) => {
          if (res.confirm) {
            // 清除用户信息
            uni.removeStorageSync('userInfo')
            uni.removeStorageSync('token')
            
            // 跳转到登录页
            uni.reLaunch({
              url: '/src/pages/login/index'
            })
          }
        }
      })
    },

    // 调试用：显示当前登录状态
    showLoginStatus() {
      const token = uni.getStorageSync('token')
      const userInfo = uni.getStorageSync('userInfo')
      const isLogin = uni.getStorageSync('isLogin')
      
      uni.showModal({
        title: '登录状态调试',
        content: `Token: ${token ? '已设置' : '未设置'}\nUserInfo: ${userInfo ? '已设置' : '未设置'}\nIsLogin: ${isLogin ? '是' : '否'}`,
        showCancel: false
      })
    }
  }
}
</script>

<style lang="scss">
.user-container {
  min-height: 100vh;
  background: linear-gradient(180deg, #F8FAFC 0%, #EDF2F7 100%);
}

.user-header {
  position: relative;
  background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
  padding: 60rpx 30rpx 40rpx;
  color: #fff;
  overflow: hidden;
}

.header-bg {
  position: absolute;
  top: 0;
  left: 0;
  right: 0;
  bottom: 0;
  background-image: radial-gradient(circle at 20% 80%, rgba(255, 255, 255, 0.1) 0%, transparent 50%),
                    radial-gradient(circle at 80% 20%, rgba(255, 255, 255, 0.1) 0%, transparent 50%);
}

.user-info {
  position: relative;
  display: flex;
  align-items: center;
  margin-bottom: 40rpx;
  z-index: 2;
}

.avatar-container {
  position: relative;
  margin-right: 30rpx;
}

.avatar {
  width: 120rpx;
  height: 120rpx;
  border-radius: 60rpx;
  border: 4rpx solid rgba(255, 255, 255, 0.2);
}

.avatar-ring {
  position: absolute;
  top: -8rpx;
  left: -8rpx;
  width: 136rpx;
  height: 136rpx;
  border-radius: 68rpx;
  border: 2rpx solid rgba(255, 255, 255, 0.3);
  animation: pulse 2s infinite;
}

@keyframes pulse {
  0%, 100% {
    transform: scale(1);
    opacity: 1;
  }
  50% {
    transform: scale(1.05);
    opacity: 0.7;
  }
}

.info {
  flex: 1;
}

.nickname {
  font-size: 36rpx;
  font-weight: bold;
  margin-bottom: 12rpx;
}

.phone {
  font-size: 28rpx;
  opacity: 0.8;
  margin-bottom: 12rpx;
}

.level-badge {
  display: flex;
  align-items: center;
  background: rgba(255, 215, 0, 0.2);
  padding: 6rpx 12rpx;
  border-radius: 16rpx;
  width: fit-content;
  
  text {
    font-size: 22rpx;
    margin-left: 6rpx;
    color: #FFD700;
    font-weight: 500;
  }
}

.settings-btn {
  padding: 10rpx;
  border-radius: 50%;
  background: rgba(255, 255, 255, 0.1);
}

.user-stats {
  position: relative;
  display: flex;
  background: rgba(255, 255, 255, 0.15);
  border-radius: 20rpx;
  padding: 30rpx 0;
  backdrop-filter: blur(10rpx);
  z-index: 2;
}

.stat-item {
  flex: 1;
  display: flex;
  flex-direction: column;
  align-items: center;
  cursor: pointer;
  transition: transform 0.2s ease;
}

.stat-item:active {
  transform: scale(0.95);
}

.stat-item .number {
  font-size: 32rpx;
  font-weight: bold;
  margin-bottom: 8rpx;
}

.stat-item .label {
  font-size: 24rpx;
  opacity: 0.9;
}

.stat-divider {
  width: 1rpx;
  height: 60rpx;
  background: rgba(255, 255, 255, 0.2);
  align-self: center;
}

.section {
  background: #fff;
  margin: 20rpx 30rpx;
  border-radius: 20rpx;
  box-shadow: 0 4rpx 20rpx rgba(0, 0, 0, 0.08);
  overflow: hidden;
}

.section-header {
  display: flex;
  justify-content: space-between;
  align-items: center;
  padding: 30rpx;
  border-bottom: 1px solid #F7FAFC;
}

.section-title {
  display: flex;
  align-items: center;
  
  text {
    font-size: 32rpx;
    font-weight: bold;
    color: #2D3748;
    margin-left: 12rpx;
  }
}

.more-btn {
  display: flex;
  align-items: center;
  
  text {
    font-size: 26rpx;
    color: #718096;
    margin-right: 8rpx;
  }
}

.order-tabs {
  display: flex;
  padding: 30rpx;
}

.tab-item {
  flex: 1;
  display: flex;
  flex-direction: column;
  align-items: center;
  transition: transform 0.2s ease;
}

.tab-item:active {
  transform: scale(0.95);
}

.tab-icon {
  width: 80rpx;
  height: 80rpx;
  border-radius: 20rpx;
  display: flex;
  align-items: center;
  justify-content: center;
  margin-bottom: 15rpx;
}

.tab-icon.pending {
  background: linear-gradient(135deg, #FFF3E0, #FFE0B2);
}

.tab-icon.shipped {
  background: linear-gradient(135deg, #E3F2FD, #BBDEFB);
}

.tab-icon.completed {
  background: linear-gradient(135deg, #E8F5E8, #C8E6C9);
}

.tab-icon.refund {
  background: linear-gradient(135deg, #FFEBEE, #FFCDD2);
}

.tab-item text {
  font-size: 24rpx;
  color: #4A5568;
  font-weight: 500;
}

.menu-grid {
  padding: 0 30rpx;
}

.menu-item {
  display: flex;
  align-items: center;
  padding: 30rpx 0;
  border-bottom: 1px solid #F7FAFC;
  transition: background-color 0.2s ease;
}

.menu-item:last-child {
  border-bottom: none;
}

.menu-item:active {
  background-color: #F7FAFC;
}

.menu-icon {
  width: 60rpx;
  height: 60rpx;
  border-radius: 15rpx;
  display: flex;
  align-items: center;
  justify-content: center;
  margin-right: 24rpx;
}

.menu-icon.address {
  background: linear-gradient(135deg, #FFE5E5, #FFF0F0);
}

.menu-icon.favorite {
  background: linear-gradient(135deg, #FFE5F4, #FFF0F8);
}

.menu-icon.wallet {
  background: linear-gradient(135deg, #E5F9E5, #F0FFF0);
}

.menu-icon.service {
  background: linear-gradient(135deg, #E5F0FF, #F0F7FF);
}

.menu-icon.help {
  background: linear-gradient(135deg, #F0E5FF, #F7F0FF);
}

.menu-icon.about {
  background: linear-gradient(135deg, #FFF5E5, #FFF9F0);
}

.menu-icon.wechat {
  background: linear-gradient(135deg, #E5F9E5, #F0FFF0);
}

.menu-content {
  flex: 1;
}

.menu-title {
  font-size: 30rpx;
  color: #2D3748;
  font-weight: 500;
  margin-bottom: 6rpx;
  display: block;
}

.menu-desc {
  font-size: 24rpx;
  color: #718096;
  display: block;
}

.logout-section {
  margin-bottom: 40rpx;
}

.logout-btn {
  width: calc(100% - 60rpx);
  height: 88rpx;
  background: linear-gradient(135deg, #FF6B6B, #FF5252);
  color: #fff;
  font-size: 32rpx;
  border-radius: 44rpx;
  margin: 30rpx;
  border: none;
  display: flex;
  align-items: center;
  justify-content: center;
  box-shadow: 0 4rpx 15rpx rgba(255, 107, 107, 0.3);
  transition: all 0.3s ease;
  
  text {
    margin-left: 12rpx;
    font-weight: 500;
  }
}

.logout-btn:active {
  transform: scale(0.98);
  box-shadow: 0 2rpx 8rpx rgba(255, 107, 107, 0.5);
}

.logout-btn::after {
  border: none;
}

.bottom-safe {
  height: env(safe-area-inset-bottom);
  height: constant(safe-area-inset-bottom);
}
</style> 